package io.github.dsl.conf

class RuleSpec extends Script {
  String name
  Closure<Boolean> when
  Closure<Boolean> _pass
  Closure<Boolean> reject
  Closure<Boolean> retrieve

  def name(String name) {
    this.name = name
  }

  void when(Closure<Boolean> closure) { this.when = closure }

  void reject(Closure<Boolean> closure) { this.reject = closure }

  void pass(Closure<Boolean> closure) { this._pass = closure }

  void retrieve(Closure<Boolean> closure) { this.retrieve = closure }

  def run() {
    boolean isReject = false
    if (when != null && !when.call()) {
      return isReject
    }
    if (reject != null) {
      isReject = reject.call()
    } else if (_pass != null) {
      isReject = !_pass()
    }

    if (isReject && retrieve != null) {
      isReject = !retrieve.call()
    }

    return isReject
  }
}